package com.by.picture.demos.web;

/**
 * @version 1.0
 * @Author 彭彦彬
 * @Date 2025/4/7 17:48
 * @注释
 */

import com.by.picture.demos.service.impl.ImageStorageService;
import com.by.picture.demos.utils.FileUploadUtil;
import org.aspectj.lang.annotation.AfterReturning;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;
@RestController
public class HelloController {
    @Autowired
    private ImageStorageService imageStorageService;
    @PostMapping("/upload")
    public String upload(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename().trim();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        String prefix = UUID.randomUUID().toString();
        String fileName = prefix + suffix;

        // 调用工具类上传文件并获取结果
        String result = FileUploadUtil.uploadFile(file, fileName);

        if (result.startsWith("http")) {
            // 保存图片信息到数据库
            imageStorageService.saveImageInfo(
                    fileName,
                    originalFilename,
                    result,
                    file.getSize(),
                    file.getContentType(),
                    "user123" // 替换为实际上传者信息
            );
            return result; // 返回图片 URL
        } else {
            return "error"; // 返回失败信息
        }
    }
}